# Copyright (c) 2021 Huawei Device Co., Ltd.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ohos makefile to build kernel

PRODUCT_NAME=${DEVICE_NAME}
OHOS_BUILD_HOME := ${ROOT_DIR}
#export KERNEL_SRC_DIR=out/KERNEL_OBJ/kernel/src_tmp/${KERNEL_VERSION}

KERNEL_PATCH_PATH := $(OHOS_BUILD_HOME)/kernel/linux/patches/${KERNEL_VERSION}
#KERNEL_CONFIG_PATH := $(OHOS_BUILD_HOME)/kernel/linux/config/${KERNEL_VERSION}
PREBUILTS_GCC_DIR := $(OHOS_BUILD_HOME)/prebuilts/gcc
CLANG_HOST_TOOLCHAIN := $(OHOS_BUILD_HOME)/prebuilts/clang/ohos/linux-x86_64/llvm/bin
KERNEL_HOSTCC := $(CLANG_HOST_TOOLCHAIN)/clang
KERNEL_PREBUILT_MAKE := make
CLANG_CC := $(CLANG_HOST_TOOLCHAIN)/clang
CLANG_LD := $(CLANG_HOST_TOOLCHAIN)/ld.lld
CLANG_AR := $(CLANG_HOST_TOOLCHAIN)/llvm-ar
CLANG_NM := $(CLANG_HOST_TOOLCHAIN)/llvm-nm
CLANG_STRIP := $(CLANG_HOST_TOOLCHAIN)/llvm-strip
CLANG_OBJCOPY := $(CLANG_HOST_TOOLCHAIN)/llvm-objcopy
CLANG_OBJDUMP := $(CLANG_HOST_TOOLCHAIN)/llvm-objdump
CLANG_READELF := $(CLANG_HOST_TOOLCHAIN)/llvm-readelf

KERNEL_CROSS_COMPILE :=
KERNEL_ARCH=x86_64
KERNEL_TARGET_TOOLCHAIN := gcc
KERNEL_TARGET_TOOLCHAIN_PREFIX :=

KERNEL_CROSS_COMPILE += CC="$(CLANG_CC)"

KERNEL_MAKE := \
    PATH="$(CLANG_HOST_TOOLCHAIN):$$PATH" \
    $(KERNEL_PREBUILT_MAKE)


KERNEL_IMAGE_FILE := $(KERNEL_SRC_TMP_PATH)/arch/x86/boot/$(KERNEL_IMAGE)
DEFCONFIG_FILE := ${DEVICE_DEFCONFIG}
#UNIFIED_COLLECTION_PATCH_FILE := ${OHOS_BUILD_HOME}/kernel/linux/common_modules/ucollection/apply_ucollection.sh

export KBUILD_OUTPUT=$(KERNEL_OBJ_TMP_PATH)

$(KERNEL_IMAGE_FILE):
	$(hide) echo "build kernel...PRODUCT_NAME=${PRODUCT_NAME} PRODUCT_PATH=${PRODUCT_PATH} BUILD_TYPE=${BUILD_TYPE} ohos_build_home=${OHOS_BUILD_HOME} IMAGE_PATH=${IMAGE_PATH}"
#ifeq ($(UNIFIED_COLLECTION_PATCH_FILE), $(wildcard $(UNIFIED_COLLECTION_PATCH_FILE)))
#	$(hide) $(UNIFIED_COLLECTION_PATCH_FILE) $(OHOS_BUILD_HOME) $(KERNEL_SRC_TMP_PATH) $(DEVICE_NAME) $(KERNEL_VERSION)
#endif
#	$(hide) cp -rf $(KERNEL_CONFIG_PATH)/. $(KERNEL_SRC_TMP_PATH)/
	$(hide) $(KERNEL_MAKE) -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) distclean
	$(hide) $(KERNEL_MAKE) -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(DEFCONFIG_FILE)
ifeq ($(KERNEL_VERSION), linux-5.10)
	$(hide) $(KERNEL_MAKE) -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) modules_prepare
endif
	$(hide) $(KERNEL_MAKE) -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) -j64 $(KERNEL_IMAGE)
	$(hide) $(KERNEL_MAKE) -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) -j64 modules
	$(hide) $(KERNEL_MAKE) INSTALL_PATH=${IMAGE_PATH} -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) -j64 install
	$(hide) $(KERNEL_MAKE) INSTALL_MOD_PATH=${IMAGE_PATH} -C $(KERNEL_SRC_TMP_PATH) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) -j64 modules_install
	$(hide) rm -rf ${IMAGE_PATH}/lib/modules/*/build
	$(hide) rm -rf ${IMAGE_PATH}/lib/modules/*/source

.PHONY: build-kernel
build-kernel: $(KERNEL_IMAGE_FILE)
